<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="任务调度" tabindex="-1"><a class="header-anchor" href="#任务调度"><span>任务调度</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#defining-schedules">定义调度</a>
<ul>
<li><a href="#scheduling-artisan-commands">调度 Artisan 命令</a></li>
<li><a href="#scheduling-queued-jobs">调度队列作业</a></li>
<li><a href="#scheduling-shell-commands">调度 Shell 命令</a></li>
<li><a href="#schedule-frequency-options">调度频率选项</a></li>
<li><a href="#timezones">时区</a></li>
<li><a href="#preventing-task-overlaps">避免任务重复</a></li>
<li><a href="#running-tasks-on-one-server">只在一台服务器上运行任务</a></li>
<li><a href="#background-tasks">后台任务</a></li>
<li><a href="#maintenance-mode">维护模式</a></li>
</ul>
</li>
<li><a href="#running-the-scheduler">运行调度器</a>
<ul>
<li><a href="#sub-minute-scheduled-tasks">小于一分钟计划任务</a></li>
<li><a href="#running-the-scheduler-locally">本地运行调度器</a></li>
</ul>
</li>
<li><a href="#task-output">任务输出</a></li>
<li><a href="#task-hooks">任务钩子</a></li>
<li><a href="#events">事件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>过去，你可能需要在服务器上为每一个调度任务去创建一个 cron 配置项。但是这种方式很快会变得不友好，因为这些任务调度已不在源代码控制中，并且你每次都需要通过 SSH 链接登录到你的服务器中查看现有的 cron 条目或添加其他配置项。</p>
<p>Laravel 的命令调度器提供了一种全新的方法来管理服务器上的定时任务。调度器允许你在 Laravel 应用中流畅地定义命令调度。使用调度器时，只需要你服务器上一个单一的 cron 配置项。你的任务调度通常在应用程序的 <code v-pre>routes/console.php</code> 文件中定义。</p>
<h2 id="定义调度" tabindex="-1"><a class="header-anchor" href="#定义调度"><span>定义调度</span></a></h2>
<p>你可以在应用程序的 <code v-pre>routes/console.php</code> 文件中定义所有计划任务。开始前，我们先看一个示例。在这个例子中，我们将安排每天午夜调用一个闭包。在闭包中，我们将执行一个数据库查询来清除一个表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'recent_users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除了使用闭包调度外，还可以调度 <a href="https://secure.php.net/manual/en/language.oop5.magic.php#object.invoke" target="_blank" rel="noopener noreferrer">invokable 对象</a>。invokable 对象指的是包含 <code v-pre>__invoke</code> 方法的简单 PHP 类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRecentUsers</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你希望将 <code v-pre>routes/console.php</code> 文件仅用于命令定义，则可在应用程序的 <code v-pre>bootstrap/app.php</code> 文件中使用 <code v-pre>withSchedule</code> 方法来定义调度任务。该方法接受一个接收调度器实例的闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Scheduling<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token operator">-></span><span class="token function">withSchedule</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Schedule</span> <span class="token variable">$schedule</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRecentUsers</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想查看调度任务的概览和下一次运行时间，可以使用 <code v-pre>schedule:list</code> Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan schedule:list</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="调度-artisan-命令" tabindex="-1"><a class="header-anchor" href="#调度-artisan-命令"><span>调度 Artisan 命令</span></a></h3>
<p>除了调度闭包，你还可以调度 <a href="https://learnku.com/docs/laravel/11.x/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令</a> 和系统命令。例如，你可以使用 <code v-pre>command</code> 方法调度 Artisan 命令，可以使用命令名或命令类。</p>
<p>当使用命令的类名调度 Artisan 命令时，你可以传入一个命令行参数数组，这些参数将在命令被调用时提供给该命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Commands<span class="token punctuation">\</span>SendEmailsCommand</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send Taylor --force'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token class-name static-context">SendEmailsCommand</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'--force'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="调度-artisan-闭包-命令" tabindex="-1"><a class="header-anchor" href="#调度-artisan-闭包-命令"><span>调度 Artisan 闭包 命令</span></a></h4>
<p>如果你想要调度由闭包定义的 Artisan 命令，你可以在命令定义后链式调用调度相关的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'delete:recent-users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'recent_users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">purpose</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Delete recent users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你需要向闭包命令传递参数，可以将它们提提供给 <code v-pre>schedule</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send {user} {--force}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">purpose</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Send emails to the specified user'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">schedule</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'--force'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="调度队列作业" tabindex="-1"><a class="header-anchor" href="#调度队列作业"><span>调度队列作业</span></a></h3>
<p><code v-pre>job</code> 方法可用于调度 <a href="https://learnku.com/docs/laravel/11.x/queues" target="_blank" rel="noopener noreferrer">队列作业</a>。该方法为调度队列作业提供了一种便捷的方式，而无需使用 <code v-pre>call</code> 方法定义闭包来调度队列作业：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>Heartbeat</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">job</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Heartbeat</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyFiveMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可为 <code v-pre>job</code> 方法提供可选的第二和第三个参数，用于指定队列名称和队列连接，以便对作业进行排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>Heartbeat</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 将任务分派到 「sqs」连接上的「心跳」队列...</span></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">job</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Heartbeat</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'heartbeats'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'sqs'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyFiveMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="调度-shell-命令" tabindex="-1"><a class="header-anchor" href="#调度-shell-命令"><span>调度 Shell 命令</span></a></h3>
<p><code v-pre>exec</code> 方法可以用于向操作系统发出命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'node /home/forge/script.js'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="调度频率选项" tabindex="-1"><a class="header-anchor" href="#调度频率选项"><span>调度频率选项</span></a></h3>
<p>我们已经举例说明了如何配置任务在指定的时间间隔内运行。然而，你还可以为任务分配更多的任务调度频率：</p>
<table>
<thead>
<tr>
<th>Method</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>-&gt;cron('* * * * *');</code></td>
<td>按自定义 Cron 计划运行任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everySecond();</code></td>
<td>每秒运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyTwoSeconds();</code></td>
<td>每两秒运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyFiveSeconds();</code></td>
<td>每五秒运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyTenSeconds();</code></td>
<td>每十秒运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyFifteenSeconds();</code></td>
<td>每十五秒运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyTwentySeconds();</code></td>
<td>每二十秒运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyThirtySeconds();</code></td>
<td>每三十秒运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyMinute();</code></td>
<td>每分钟运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyTwoMinutes();</code></td>
<td>每两分钟运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyThreeMinutes();</code></td>
<td>每三分钟运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyFourMinutes();</code></td>
<td>每四分钟运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyFiveMinutes();</code></td>
<td>每五分钟运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyTenMinutes();</code></td>
<td>每十分钟运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyFifteenMinutes();</code></td>
<td>每十五分钟运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyThirtyMinutes();</code></td>
<td>每三十分钟运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;hourly();</code></td>
<td>每小时运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;hourlyAt(17);</code></td>
<td>每小时整点过十七分运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyOddHour($minutes = 0);</code></td>
<td>每奇数小时运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyTwoHours($minutes = 0);</code></td>
<td>每两运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyThreeHours($minutes = 0);</code></td>
<td>每三小时运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everyFourHours($minutes = 0);</code></td>
<td>每四小时运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;everySixHours($minutes = 0);</code></td>
<td>每六小时运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;daily();</code></td>
<td>每天午夜运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;dailyAt('13:00');</code></td>
<td>每天 13:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;twiceDaily(1, 13);</code></td>
<td>每天在 1:00 和 13:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;twiceDailyAt(1, 13, 15);</code></td>
<td>每天在 1:15 和 13:15 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;weekly();</code></td>
<td>每周日 00:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;weeklyOn(1, '8:00');</code></td>
<td>每周一 8:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;monthly();</code></td>
<td>每月第一天 00:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;monthlyOn(4, '15:00');</code></td>
<td>每月 4 日 15:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;twiceMonthly(1, 16, '13:00');</code></td>
<td>每月 1 日和 16 日 13:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;lastDayOfMonth('15:00');</code></td>
<td>每月最后一天 15:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;quarterly();</code></td>
<td>每个季度的第一天 00:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;quarterlyOn(4, '14:00');</code></td>
<td>每个季度的第 4 天 14:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;yearly();</code></td>
<td>每年的第一天 00:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;yearlyOn(6, 1, '17:00');</code></td>
<td>每年 6 月 1 日 17:00 运行一次任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;timezone('America/New_York');</code></td>
<td>设置任务时区</td>
</tr>
</tbody>
</table>
<p>这些方法可以与其他约束结合使用，创建更加精细的调度，只在一周的某些特定日子运行。例如，你可以调度命令在每周一运行一次：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 每周运行一次，时间为周一下午 1 点...</span></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">weekly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mondays</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">at</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'13:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 在工作日上午 8 点至下午 5 点之间，每小时运行一次...</span></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">weekdays</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">hourly</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">timezone</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'America/Chicago'</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">between</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'8:00'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'17:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>其他时间约束列表如下：</p>
<table>
<thead>
<tr>
<th>Method</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>-&gt;weekdays();</code></td>
<td>将任务限制为工作日</td>
</tr>
<tr>
<td><code v-pre>-&gt;weekends();</code></td>
<td>将任务限制到周末</td>
</tr>
<tr>
<td><code v-pre>-&gt;sundays();</code></td>
<td>将任务限制到周日</td>
</tr>
<tr>
<td><code v-pre>-&gt;mondays();</code></td>
<td>将任务限制在星期一</td>
</tr>
<tr>
<td><code v-pre>-&gt;tuesdays();</code></td>
<td>将任务限制在星期二</td>
</tr>
<tr>
<td><code v-pre>-&gt;wednesdays();</code></td>
<td>将任务限制在星期三</td>
</tr>
<tr>
<td><code v-pre>-&gt;thursdays();</code></td>
<td>将任务限制在星期四</td>
</tr>
<tr>
<td><code v-pre>-&gt;fridays();</code></td>
<td>将任务限制在星期五</td>
</tr>
<tr>
<td><code v-pre>-&gt;saturdays();</code></td>
<td>将任务限制在星期六</td>
</tr>
<tr>
<td>`-&gt;days(array</td>
<td>mixed);`</td>
</tr>
<tr>
<td><code v-pre>-&gt;between($startTime, $endTime);</code></td>
<td>限制任务在开始时间和结束时间之间运行</td>
</tr>
<tr>
<td><code v-pre>-&gt;unlessBetween($startTime, $endTime);</code></td>
<td>限制任务不在开始时间和结束时间之间运行</td>
</tr>
<tr>
<td><code v-pre>-&gt;when(Closure);</code></td>
<td>根据真值测试限制任务</td>
</tr>
<tr>
<td><code v-pre>-&gt;environments($env);</code></td>
<td>将任务限制在特定环境中</td>
</tr>
</tbody>
</table>
<h4 id="日约束" tabindex="-1"><a class="header-anchor" href="#日约束"><span>日约束</span></a></h4>
<p><code v-pre>days</code> 方法可以用于约束任务在每周的指定日期执行。举个例子，你可以调度命令在星期天和星期三每小时运行一次：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">hourly</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">days</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>不仅如此，你还可以使用 <code v-pre>Illuminate\Console\Scheduling\Schedule</code> 类中的常量来设置任务在指定日期运行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Scheduling<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name class-name-fully-qualified static-context">Facades<span class="token punctuation">\</span>Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">hourly</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">days</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token constant">SUNDAY</span><span class="token punctuation">,</span> <span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token constant">WEDNESDAY</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="时间范围限制" tabindex="-1"><a class="header-anchor" href="#时间范围限制"><span>时间范围限制</span></a></h4>
<p><code v-pre>between</code> 方法可用于限制任务在一天中的某个时间段执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">hourly</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">between</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'7:00'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'22:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同样，<code v-pre>unlessBetween</code> 方法也可用于限制任务不在一天中的某个时间段执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">hourly</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">unlessBetween</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'23:00'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'4:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="真值检测限制" tabindex="-1"><a class="header-anchor" href="#真值检测限制"><span>真值检测限制</span></a></h4>
<p><code v-pre>when</code> 方法可根据闭包返回结果来执行任务。换言之，若给定的闭包返回 <code v-pre>true</code>，若无其他限制条件阻止，任务就会一直执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">when</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>skip</code> 可看作是 <code v-pre>when</code> 的逆方法。若 <code v-pre>skip</code> 方法返回 <code v-pre>true</code>，任务将不会执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当链式调用 <code v-pre>when</code> 方法时，仅当所有<code v-pre>when</code> 都返回 <code v-pre>true</code> 时，任务才会执行。</p>
<h4 id="环境限制" tabindex="-1"><a class="header-anchor" href="#环境限制"><span>环境限制</span></a></h4>
<p><code v-pre>environments</code> 方法可用于指定任务的执行环境（由 <code v-pre>APP_ENV</code> <a href="https://learnku.com/docs/laravel/11.x/configuration#environment-configuration" target="_blank" rel="noopener noreferrer">环境变量</a>定义):</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">environments</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'staging'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'production'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="时区" tabindex="-1"><a class="header-anchor" href="#时区"><span>时区</span></a></h3>
<p>使用 <code v-pre>timezone</code> 方法，你可以指定计划任务的时间转化为给定的时区：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'report:generate'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">timezone</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'America/New_York'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">at</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'2:00'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你要重复为所有计划任务分配相同的时区，可以在应用程序的 <code v-pre>app</code> 配置文件中定义一个 <code v-pre>schedule_timezone</code> 选项，指定应将哪个时区分配给所有计划任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'timezone'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_TIMEZONE'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'UTC'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'schedule_timezone'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'America/Chicago'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!WARNING]<br>
请记住，有些时区使用夏令时。当夏令时发生变化时，你的计划任务可能会运行两次，甚至根本不会运行。因此，我们建议尽可能避免时区调度。</p>
</blockquote>
<h3 id="避免任务重复" tabindex="-1"><a class="header-anchor" href="#避免任务重复"><span>避免任务重复</span></a></h3>
<p>默认情况下，即使之前的任务实例还在执行，计划内的任务也会执行。为避免这种情况的发生，你可以使用 <code v-pre>withoutOverlapping</code> 方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withoutOverlapping</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在此例中，若 <code v-pre>emails:send</code> <a href="https://learnku.com/docs/laravel/11.x/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令</a> 尚未运行，则它将会每分钟执行一次。如果任务的执行时间非常不确定，导致你无法准确预测任务的执行时间，那么 <code v-pre>withoutOverlapping</code> 方法就特别有用。</p>
<p>如有需要，你可以在 <code v-pre>withoutOverlapping</code> 锁过期之前，指定它的过期分钟数。默认情况下，这个锁会在 24 小时后过期：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withoutOverlapping</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在幕后，withoutOverlapping 方法使用应用程序的 <a href="https://learnku.com/docs/laravel/11.x/cache" target="_blank" rel="noopener noreferrer">cache</a> 来获取锁。如果必要，你可以使用 <code v-pre>schedule:clear-cache</code> Artisan 命令清除这些缓存锁。通常只有在服务器出现意外问题导致任务卡住时才需要这样做。</p>
<h3 id="任务只运行在一台服务器上" tabindex="-1"><a class="header-anchor" href="#任务只运行在一台服务器上"><span>任务只运行在一台服务器上</span></a></h3>
<blockquote>
<p>[!WARNING]<br>
要使用此功能，你的应用程序必须使用 <code v-pre>database</code>、<code v-pre>memcached</code>、<code v-pre>dynamodb</code> 或 <code v-pre>redis</code> 缓存驱动程序作为应用程序的默认缓存驱动程序。此外，所有服务器都必须与同一个中央缓存服务器通信。</p>
</blockquote>
<p>如果你的应用程序的调度器在多台服务器上运行，你可以限制计划任务只在一台服务器上执行。例如，假设你有一个计划任务，每周五晚上生成一份新报告。如果任务调度器在三个工作服务器上运行，则计划任务将在所有三个服务器上运行并三次生成报告。这不好！</p>
<p>要表示任务仅在一台服务器上运行，请在定义计划任务时使用 <code v-pre>onOneServer</code> 方法。第一个获得任务的服务器将确保任务的原子锁，以防止其他服务器同时运行同一任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'report:generate'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">fridays</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">at</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'17:00'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onOneServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="为单服务器任务命名" tabindex="-1"><a class="header-anchor" href="#为单服务器任务命名"><span>为单服务器任务命名</span></a></h4>
<p>有时，你可能需要使用不同的参数调度相同的作业，同时指示 Laravel 在单服务器上运行每个作业的每种排列。要做到这一点，你可以通过 <code v-pre>name</code> 方法为每个计划定义分配唯一名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">job</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">CheckUptime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://laravel.com'</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'check_uptime:laravel.com'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">everyFiveMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">onOneServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">job</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">CheckUptime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://vapor.laravel.com'</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'check_uptime:vapor.laravel.com'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">everyFiveMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">onOneServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同样，如果打算在一台服务器上运行计划闭包，则必须为其分配一个名称:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">resetApiRequestCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'reset-api-request-count'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">onOneServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="后台任务" tabindex="-1"><a class="header-anchor" href="#后台任务"><span>后台任务</span></a></h3>
<p>默认情况下，计划在同一时间执行的多个任务将根据你的 <code v-pre>schedule</code> 方法中定义的顺序依次执行。如果你有长时间运行的任务，将会导致后续任务比预期时间更晚启动。如果你想在后台运行任务，以便它们同时运行，你可以使用 <code v-pre>runInBackground</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'analytics:report'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">runInBackground</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!WARNING]<br>
<code v-pre>runInBackground</code> 方法只能在通过 <code v-pre>command</code> 和 <code v-pre>exec</code> 方法调度任务时使用。</p>
</blockquote>
<h3 id="维护模式" tabindex="-1"><a class="header-anchor" href="#维护模式"><span>维护模式</span></a></h3>
<p>当应用程序处于<a href="https://learnku.com/docs/laravel/11.x/configuration#maintenance-mode" target="_blank" rel="noopener noreferrer">维护模式</a>时，你的应用程序计划任务将不会运行，因为我们不想调度任务干扰到服务器上可能还未完成的维护项目。然而，如果你想强制任务在维护模式下运行，你可以使用 <code v-pre>evenInMaintenanceMode</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">evenInMaintenanceMode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="运行调度器" tabindex="-1"><a class="header-anchor" href="#运行调度器"><span>运行调度器</span></a></h2>
<p>现在，我们已经学会了如何定义计划任务，接下来让我们讨论如何真正在服务器上运行它们。<code v-pre>schedule:run</code> Artisan 命令将评估你的所有计划任务，并根据服务器的当前时间决定它们是否运行。</p>
<p>因此，当使用 Laravel 的调度器时，我们只需要在服务器上添加一个单一的 cron 配置项，每分钟运行一次 <code v-pre>schedule:run</code> 命令。如果你不知道如何向服务器添加 cron 条目，请考虑使用诸如 <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a> 之类的服务来为你管理 cron 配置项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">* * * * * <span class="token builtin class-name">cd</span> /path-to-your-project <span class="token operator">&amp;&amp;</span> php artisan schedule:run <span class="token operator">>></span> /dev/null <span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token file-descriptor important">&amp;1</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="小于一分钟的预定任务" tabindex="-1"><a class="header-anchor" href="#小于一分钟的预定任务"><span>小于一分钟的预定任务</span></a></h3>
<p>在大多数操作系统上，cron 作业被限制为每分钟最多运行一次。然而，Laravel 的调度程序允许你安排任务以更频繁的间隔运行，甚至频繁到每秒一次:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'recent_users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everySecond</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当您的应用程序中定义了小于一分钟的任务时，<code v-pre>schedule:run</code> 命令将继续运行到当前分钟结束而不是立即退出。这允许命令在整个分钟内调用所有需要的小于一分钟的任务。</p>
<p>由于小于一分钟的任务如果运行时间超出预期可能会延迟后续小于一分钟任务的执行，建议所有小于一分钟的任务派发队列作业或后台命令来负责实际的任务处理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>DeleteRecentUsers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">job</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRecentUsers</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyTenSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users:delete'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyTenSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">runInBackground</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="中断小于一分钟的任务" tabindex="-1"><a class="header-anchor" href="#中断小于一分钟的任务"><span>中断小于一分钟的任务</span></a></h4>
<p>由于在定义了小于一分钟的任务时，<code v-pre>schedule:run</code> 命令会在调用的整分钟内运行，因此有时可能需要在部署应用程序时中断该命令。否则，已在运行的 <code v-pre>schedule:run</code> 命令实例将继续使用你的应用程序先前部署的代码，直到当前分钟结束。</p>
<p>为了中断正在进行的 <code v-pre>schedule:run</code> 调用，你可添加 <code v-pre>schedule:interrupt</code> 命令到应用程序的部署脚本中。该命令应在你的应用程序部署完成后调用：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan schedule:interrupt</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="本地运行调度器" tabindex="-1"><a class="header-anchor" href="#本地运行调度器"><span>本地运行调度器</span></a></h3>
<p>通常，你不会在本地开发机器上添加调度器 cron 条目。相反，你可以使用 <code v-pre>schedule:work</code> Artisan 命令。此命令将在前台运行，并在你终止命令之前每分钟调用一次调度器：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan schedule:work</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="任务输出" tabindex="-1"><a class="header-anchor" href="#任务输出"><span>任务输出</span></a></h2>
<p>Laravel 调度器提供了几种方便的方法来处理计划任务生成的输出。首先，使用 <code v-pre>sendOutputTo</code> 方法，你可以将输出发送到文件，以供日后检查：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">sendOutputTo</span><span class="token punctuation">(</span><span class="token variable">$filePath</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你希望将输出追加到给定文件，你可以使用 <code v-pre>appendOutputTo</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">appendOutputTo</span><span class="token punctuation">(</span><span class="token variable">$filePath</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 <code v-pre>emailOutputTo</code> 方法, 你可以把输出结果电邮到你选择的电邮地址。在发送任务的输出结果之前，你应该配置 Laravel 的 <a href="https://learnku.com/docs/laravel/11.x/mail" target="_blank" rel="noopener noreferrer">电子邮件服务</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'report:generate'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">sendOutputTo</span><span class="token punctuation">(</span><span class="token variable">$filePath</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">emailOutputTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你只想在预定的 Artisan 或系统命令以非零退出代码终止时通过电子邮件发送输出，请使用 <code v-pre>emailOutputOnFailure</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'report:generate'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">emailOutputOnFailure</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!WARNING]<br>
<code v-pre>emailOutputTo</code> ， <code v-pre>emailOutputOnFailure</code> ， <code v-pre>sendOutputTo</code> 和 <code v-pre>appendOutputTo</code> 方法是 <code v-pre>command</code> 和 <code v-pre>exec</code> 方法所独有的。</p>
</blockquote>
<h2 id="任务钩子" tabindex="-1"><a class="header-anchor" href="#任务钩子"><span>任务钩子</span></a></h2>
<p>使用 <code v-pre>before</code> 和 <code v-pre>after</code> 方法，你可以指定在计划任务执行前后要执行的代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">before</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务即将执行...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务已执行...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>onSuccess</code> 和 <code v-pre>onFailure</code> 方法允许你指定在计划任务成功或失败时执行的代码。失败表示预定的 Artisan 或系统命令以非零的退出代码终止：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">onSuccess</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务成功...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">onFailure</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务失败...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果命令中提供了输出，你可以在 <code v-pre>after</code>、<code v-pre>onSuccess</code> 或 <code v-pre>onFailure</code> 钩子中访问它，方法是将 <code v-pre>Illuminate\Support\Stringable</code> 实例作为钩子闭包定义的 <code v-pre>$output</code> 参数进行类型提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">onSuccess</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$output</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务成功...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">onFailure</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$output</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">             <span class="token comment">// 任务失败...</span></span>
<span class="line">         <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ping-网址" tabindex="-1"><a class="header-anchor" href="#ping-网址"><span>Ping 网址</span></a></h4>
<p>使用 <code v-pre>pingBefore</code> 和 <code v-pre>thenPing</code> 方法，调度器可以在任务执行前后自动 ping 给定的网址。此方法对于通知你计划任务正在开始或已完成执行的外部服务（如 <a href="https://envoyer.io/" target="_blank" rel="noopener noreferrer">Envoyer</a>）非常有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">pingBefore</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">thenPing</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>pingBeforeIf</code> 和 <code v-pre>thenPingIf</code> 方法可用于仅在给定条件为 <code v-pre>true</code> 时 ping 给定网址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">pingBeforeIf</span><span class="token punctuation">(</span><span class="token variable">$condition</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">thenPingIf</span><span class="token punctuation">(</span><span class="token variable">$condition</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>pingOnSuccess</code> 和 <code v-pre>pingOnFailure</code> 方法可用于仅在任务成功或失败时 ping 给定的网址。失败表示预定的 Artisan 或系统命令以非零退出代码终止：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">pingOnSuccess</span><span class="token punctuation">(</span><span class="token variable">$successUrl</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">pingOnFailure</span><span class="token punctuation">(</span><span class="token variable">$failureUrl</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>Laravel 在调度过程中发送各种<a href="https://learnku.com/docs/laravel/11.x/events" target="_blank" rel="noopener noreferrer">事件</a>。你可以为以下事件<a href="https://learnku.com/docs/laravel/11.x/events" target="_blank" rel="noopener noreferrer">定义监听器</a>：</p>
<table>
<thead>
<tr>
<th>Event Name</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>Illuminate\Console\Events\ScheduledTaskStarting</code></td>
</tr>
<tr>
<td><code v-pre>Illuminate\Console\Events\ScheduledTaskFinished</code></td>
</tr>
<tr>
<td><code v-pre>Illuminate\Console\Events\ScheduledBackgroundTaskFinished</code></td>
</tr>
<tr>
<td><code v-pre>Illuminate\Console\Events\ScheduledTaskSkipped</code></td>
</tr>
<tr>
<td><code v-pre>Illuminate\Console\Events\ScheduledTaskFailed</code></td>
</tr>
</tbody>
</table>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/schedulingmd/16689" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/sc...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/schedulingmd/16689" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/sc...</a></p>
</blockquote>
</div></template>


